/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x10.event;

import java.io.Serializable;

import x10.enums.Interfaces;
import x10.utilities.ChunkOfBytes;

/**
 *
 * @author Denny
 */
public class X10InterfaceMemoryDumpRequest extends X10Event implements Serializable{
    static final long serialVersionUID = 1L;
    private ChunkOfBytes bytes = new ChunkOfBytes();
    byte memoryDumpRequestByte = (byte) 0xDB;
    byte startHigh = (byte) 0xFF;
    byte startLow = (byte) 0xF1;
    private String address;
    private String source;
    private long instantiatedMillis;

    public X10InterfaceMemoryDumpRequest(){
	//TODO: Fix the constructor on this to accept the parameters
	
	this.source = Interfaces.CM15A.toString();
        this.instantiatedMillis = System.currentTimeMillis();
        this.address ="MEMDUMP";
        byte[] temp = new byte[3];
        temp[0] = memoryDumpRequestByte;  //Valid for at least the CM15A
        temp[1] = startHigh;
        temp[2] = startLow;
        bytes.setBytes(temp);
        bytes.setLength(3);
        bytes.setEmpty(false);
    }

    /**
     * @return the source
     */
    public String getSource() {
        return source;
    }

    /**
     * @param source the source to set
     */
    public void setSource(String source) {
        this.source = source;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the instantiatedMillis
     */
    public long getInstantiatedMillis() {
        return instantiatedMillis;
    }

    /**
     * @param instantiatedMillis the instantiatedMillis to set
     */
    public void setInstantiatedMillis(long instantiatedMillis) {
        this.instantiatedMillis = instantiatedMillis;
    }

    public ChunkOfBytes getBytes() {
        return bytes;
    }

    public void setBytes(ChunkOfBytes bytes) {
        this.bytes = bytes;
    }

    public boolean equals(X10Event device){
        boolean isEqual = true;

        if(device instanceof X10InterfaceMemoryDumpRequest){
            X10InterfaceMemoryDumpRequest other = (X10InterfaceMemoryDumpRequest)device;
            if(other!=null){
                //If both objects are within 1 second of each other, call them equal
                long low = this.getInstantiatedMillis() - 1000; //1 second earlier
                long high = this.getInstantiatedMillis() + 1000; //1 second later
                long otherMillis = other.getInstantiatedMillis();

                if(!((otherMillis >= low) && (otherMillis <= high))){
                    isEqual = false;
                }
            }
            if(!source.contentEquals(other.getSource())){
        	isEqual = false;
            }
        } else {
            isEqual = false;
        }

        return isEqual;
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append(source);
        sb.append(" MEMORY DUMP REQUEST ");
        sb.append(bytes.toString());
        return sb.toString();
    }


}
